#!/usr/bin/env python

import os, sys

if len(sys.argv) <> 6 :
	print 'cl_cpd_props.py <path to scripts> <queue> <username> <password> <project>'
	sys.exit(1)

path = sys.argv[1]
queue = sys.argv[2]
username = sys.argv[3]
password = sys.argv[4]
project = sys.argv[5]



files = os.listdir('./')

first_bit = "#BSUB-q short             	# Job queue\n#BSUB-o job.output              # output is sent to file job.output\n#BSUB-J test_mogon              # name of the job\n"



tpsa_file = first_bit + 'tpsa.py '
match_count_file = first_bit + 'match_count.py '
unwanted_file = first_bit + 'unwanted_groups.py '



files.sort()

os.makedirs('filter_des')
os.makedirs('tpsa')
os.makedirs('match_count')
os.makedirs('unwanted')




for i in files:

	if i[-4:] == '.smi':
		#filter_des 

		file_name = i[:-4] + '_filter_des.bin'
		start_file = open(file_name, 'w')
		start_file.write(first_bit)
		start_file.write(path + '/filter_descriptors.py ' + i + ' filter_des/' + i[:-4] + '_filter_descriptors.txt\n')
		start_file.write(path + '/mysql_load_filter_des.py ' + username + ' ' + password + ' filter_des/' + i[:-4] + '_filter_descriptors.txt ' + project)
		start_file.close()
		os.system('chmod 744 '  + file_name)
		command = 'qsub -q ' + queue + ' ' + file_name
		print command
		os.system(command)
for i in files:

	if i[-4:] == '.smi':

		#tpsa
		file_name = i[:-4] + '_tpsa.bin'
		start_file = open(file_name, 'w')
		start_file.write(first_bit)
		start_file.write(path + '/tpsa.py --i=' + i + ' --o=tpsa/' + i[:-4] + '_tpsa.txt  --SandP\n')
		start_file.write(path + '/mysql_load_tpsa.py ' + username + ' ' + password + ' tpsa/' + i[:-4] + '_tpsa.txt ' + project)
		start_file.close()
		os.system('chmod 744 ' + file_name)
		command = 'qsub -q ' + queue + ' ' + file_name
		print command
		os.system(command)

for i in files:

	if i[-4:] == '.smi':
		#match count
		file_name = i[:-4] + '_mc.bin'
		start_file = open(file_name, 'w')
		start_file.write(first_bit)
		start_file.write(path + '/match_count.py ' + i + ' match_count/' + i[:-4] + '_match_count.txt\n')
		start_file.write(path + '/mysql_load_match_count.py ' + username + ' ' + password + ' match_count/' + i[:-4] + '_match_count.txt ' + project)
		start_file.close()
		os.system('chmod 744 ' + file_name)
		command = 'qsub -q ' + queue + ' ' + file_name
		print command
		os.system(command)

for i in files:

	if i[-4:] == '.smi':
		#unwanted groups
		file_name = i[:-4] + '_uw_start.bin'
		start_file = open(file_name, 'w')
		start_file.write(first_bit)
		start_file.write(path + '/unwanted_groups.py ' + i + ' unwanted/' + i[:-4] + '.txt\n')
		start_file.write(path + '/mysql_load_unwanted_groups.py ' + username + ' ' + password + ' unwanted/' + i[:-4] + '.txt ' + project)
		start_file.close()
		os.system('chmod 744 '  + file_name)
		command = 'qsub -q ' + queue + ' ' + file_name
		print command
		os.system(command)

	


	
	



